package com.example.listview1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;



public class MainActivity extends AppCompatActivity {
    private List<Student> studentArrayList = new ArrayList<Student>();
    private Button btnAddStu;
    private StudentAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //listView.setAdapter(adapter);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStudent(); //初始化学生数据

        adapter = new StudentAdapter(MainActivity.this, (ArrayList<Student>) studentArrayList);
        final ListView stulistView = (ListView) findViewById(R.id.list_view);
        stulistView.setAdapter(adapter);


        stulistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this,
                        "姓名"+studentArrayList.get(i).getName()+
                                "年龄"+studentArrayList.get(i).getMun(),
                        Toast.LENGTH_SHORT).show();
            }


        });

        btnAddStu = findViewById(R.id.btn_AddStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,AddStuActivity.class);
                startActivityForResult(intent,1001);
            }
        });



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if(data == null){
            return;
        }

        String name = data.getStringExtra("NAME");
        String age = data.getStringExtra("AGE");
        int  imgid = data.getIntExtra("IMGID",R.drawable.a1);

        Student s = new Student(name,imgid,age);
        studentArrayList.add(s);
        adapter.notifyDataSetChanged();


    }
/*
    初始化学生数据
 */
    private void initStudent(){
        Student stu1 = new Student("小明" ,R.drawable.a1 ,"2");
        studentArrayList.add(stu1);
        Student stu2 = new Student("小红" ,R.drawable.a2 ,"12");
        studentArrayList.add(stu2);
        Student stu3 = new Student("小绿" ,R.drawable.a3 ,"42");
        studentArrayList.add(stu3);
        Student stu4 = new Student("小橙" ,R.drawable.a4 ,"12");
        studentArrayList.add(stu4);
        Student stu5 = new Student("小紫" ,R.drawable.a5 ,"52");
        studentArrayList.add(stu5);
        Student stu6 = new Student("小蓝" ,R.drawable.a6 ,"62");
        studentArrayList.add(stu6);
        Student stu7 = new Student("小靛" ,R.drawable.a7 ,"72");
        studentArrayList.add(stu7);
        Student stu8 = new Student("小青" ,R.drawable.a8 ,"82");
        studentArrayList.add(stu8);
        Student stu9 = new Student("小黄" ,R.drawable.a9 ,"2");
        studentArrayList.add(stu9);

    }


}
